<?php

/**
 * class Presentation
 * 
 * Simple template engine
 * 
 * @author Kirill Krasnoschekov
 */
abstract class Presentation
{
    // Templates array
    protected $tmpl;
    
    // Global variables array (available in each template block)
    protected $globalVars;
    


    /**
     * Constructor
     * 
     * @param array $tmpl
     * Set of templates like "abraca{substitution_name}dabra"
     * @param array $globalVars
     * Global variables (available in each template block)
     * @author Kirill Krasnoschekov
     */
    public function __construct(array $tmpl, array $globalVars) 
    {
        $this->tmpl = $tmpl;
        $this->globalVars = $globalVars;
    }



    /**
     * Prints a header
     * 
     * @author Kirill Krasnoschekov
     */
    public function PrintHeader() 
    {
        // Dummy array
        $data = array();
        
        // Printing the header
        $this->Out($this->Substitute($data, $this->tmpl['Header']));
        
        // Printing page index
        if ($this->globalVars['pages'] > 1)
        {
            // Page index opening tags
            $this->Out($this->Substitute($data, $this->tmpl['IndexOpen']));
            
            // Links to pages
            for ($i = 1; $i < $this->globalVars['pages'] + 1; $i++)
            {
                $this->Out($this->Substitute(
                    array('curpage0' => $i - 1, 'curpage1' => $i), 
                    $this->tmpl['IndexElem']));
            }
            
            // Page index closing tags
            $this->Out($this->Substitute($data, $this->tmpl['IndexClose']));
        }
    }
    
    /**
     * Prints the messages form
     * 
     * @param array $message Array with message data (name, text)
     * @author Kirill Krasnoschekov
     */
    public function PrintMessageForm(array $message) 
    {
        $this->Out($this->Substitute($message, $this->tmpl['MessageForm']));    
    }
    
    /**
     * Prints a message
     * 
     * @param array $message Message data (timestamp, name, text)
     * @author Kirill Krasnoschekov
     */
    public function PrintMessage(array $message) 
    {
        $message['textbr'] = nl2br($message['text']);
        $this->Out($this->Substitute($message, $this->tmpl['MessageBody']));
    }
    
    /**
     * Prints the comments form
     * 
     * @param array $commrnt Array with comment data (msgid, name, text)
     * @author Kirill Krasnoschekov
     */
    public function PrintCommentForm(array $comment) 
    {
        $this->Out($this->Substitute($comment, $this->tmpl['CommentForm']));
    }

    /**
     * Prints a comment
     * 
     * @param array $comment Comment data (timestamp, msgid, name, text)
     * @author Kirill Krasnoschekov
     */    
    public function PrintComment(array $comment) 
    {
        $comment['textbr'] = nl2br($comment['text']);
        $this->Out($this->Substitute($comment, $this->tmpl['CommentBody'])); 
    }
    
    /**
     * Prints footer with elapsed time
     *
     * @param int $time
     * @author Kirill Krasnoschekov
     */
    public function PrintFooter($time) 
    {
        $this->Out($this->Substitute(array('time' => $time), $this->tmpl['Footer']));
    }


    
    /**
     * Substitutes values from $data into $template instead of {KEY}s.
     * 
     * @param array $data
     * @param string $template
     * @author Kirill Krasnoschekov
     */    
    protected function Substitute(array $data, $template)
    {
        $data += $this->globalVars;
        return preg_replace('/{(\w+)}/e', '$data["\\1"]', $template);
    }



    /*
     * Abstract functions
     */ 
    
    /**
     * Outputs (by some means) the text.
     * 
     * @param string $string String to output
     */
    protected abstract function Out($string);     
}




/**
 * class HTMLPresentation
 * 
 * @author Kirill Krasnoschekov
 */
class HTMLPresentation extends Presentation
{
    protected function Out($string)
    {
        print $string;
    }
}

?>